package com.lwl.controller;

import com.lwl.entity.LayData;
import com.lwl.entity.LayTreeData;
import com.lwl.entity.TMenu;
import com.lwl.entity.Tuser;
import com.lwl.service.TMenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (TMenu)表控制层
 *
 * @author Lei
 * @since 2022-10-24 14:43:28
 */
@Controller
@RequestMapping("tMenu")
public class TMenuController {
    /**
     * 服务对象
     */
    @Autowired
    private TMenuService tMenuService;


    /**
     *  登录页面发出的请求，根据登录用户的权限来查询它拥有的菜单，并跳转到主页面进行展示
     * @return
     */
    @GetMapping("getAll")
    public String getAll(HttpServletRequest request, HttpSession session){
//        登录的时候要根据当前登录的员工的id，查询它所拥有的菜单的id进行展示
        Tuser user = (Tuser) session.getAttribute("user");
        List<TMenu> menus = tMenuService.queryAll(user.getRid());
//        System.out.println("menus = " + menus);
        request.setAttribute("menus",menus);
        return "main";
    }

    /**
     * 查询树形菜单的数据，因为layui树形菜单数据有一定的格式，所以这里新定义了一个实体类，LayTreeData
     * @return
     */
    @GetMapping("treeData")
    @ResponseBody
    public LayData getAllMenu(){
        List<LayTreeData> treeDataList = tMenuService.getAllTreeMenu();
        return new LayData(treeDataList);
    }


    /**
     * 查询当前角色拥有的菜单
     * @return
     */
    @GetMapping("checkData/{rid}")
    @ResponseBody
    public LayData checkData(@PathVariable Integer rid){
        List<Integer> checkDataList = tMenuService.getCheckDataList(rid);
        return new LayData(checkDataList);
    }


}
